package gao.xiaolei.controller;

import gao.xiaolei.dto.TestDesignInDto;
import gao.xiaolei.dto.TestDesignOutDto;
import gao.xiaolei.dto.TestTreeDto;
import gao.xiaolei.entity.ResponseEnvelope;
import gao.xiaolei.entity.TestStep;
import gao.xiaolei.exception.AuthException;
import gao.xiaolei.exception.LeafException;
import gao.xiaolei.exception.StatusException;
import gao.xiaolei.service.TestDesignService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/common/testDesign")
public class TestDesignController {

    @Resource(name = "TestDesignService")
    private TestDesignService testDesignService;

    @PostMapping("")
    public void addTestDesign(@RequestBody TestDesignInDto testDesignInDto) {
        testDesignService.addTestDesign(testDesignInDto);
    }

    @DeleteMapping("/{id}/{userId}")
    public void deleteTestDesign(@PathVariable("id") String id,
                                 @PathVariable("userId") String userId) {
        testDesignService.deleteTestDesign(id, userId);
    }

    @PutMapping("")
    public void updateTestDesign(@RequestBody TestDesignInDto testDesignInDto) {
        testDesignService.updateTestDesign(testDesignInDto);
    }

    @PutMapping("/title")
    public void updateTitle(@RequestParam("id") String id,
                            @RequestParam("text") String title,
                            @RequestParam("userId") String userId) {
        testDesignService.updateTestDesignTitle(id, title, userId);
    }

    @PutMapping("/parent")
    public void updateParent(@RequestParam("id") String id,
                             @RequestParam("newParentId") String newParentId,
                             @RequestParam("userId") String userId) {
        testDesignService.updateParentId(id, newParentId, userId);
    }

    //给测试执行提供关联测试计划的选择（只提供审核通过的测试计划）
    @GetMapping("/project/isPass/{projectId}")
    public List<TestTreeDto> getRootSubjectForTestRun(@PathVariable("projectId") String projectId) {
        return testDesignService.getRootSubjectForTestRun(projectId);
    }

    @GetMapping("/project/{projectId}/{userId}")
    public List<TestTreeDto> getRootSubject(@PathVariable("projectId") String projectId,
                                            @PathVariable("userId") String userId) {
        return testDesignService.getRootSubject(projectId, userId);
    }

    @GetMapping("/project/transform/{projectId}")
    public List<TestTreeDto> getRootSubjectForTransform(@PathVariable("projectId") String projectId) {
        return testDesignService.getRootSubjectForTransform(projectId);
    }

    @GetMapping("/children/{id}/{userId}")
    public List<TestTreeDto> getChildren(@PathVariable("id") String id,
                                         @PathVariable("userId") String userId) {
        return testDesignService.getChildren(id, userId);
    }

    @GetMapping("/childrenIsSubject/{id}")
    public List<TestTreeDto> getChildrenIsSubject(@PathVariable("id") String id) {
        return testDesignService.getChildrenIsSubject(id);
    }

    //获取某个节点下的所有能显示的节点(只有已经审核通过了的和文件夹，供测试执行选择测试计划使用)
    @GetMapping("/childrenIsPass/{id}")
    public List<TestTreeDto> getChildrenIsPass(@PathVariable("id") String id) {
        return testDesignService.getChildrenIsPass(id);
    }


    @GetMapping("/detail/{id}")
    public TestDesignOutDto getDetail(@PathVariable("id") String id) {
        return testDesignService.getDetail(id);
    }

    //用于给测试执行获取测试计划的步骤内容
    @GetMapping("/step/{testDesignId}")
    public List<TestStep> getStep(@PathVariable("testDesignId") String testDesignId) {
        return testDesignService.getStep(testDesignId);
    }

    //将该测试计划转换成对应的测试执行
    @PostMapping("/transformToRun")
    public void transformToDesign(@RequestParam(name = "id") String id,
                                  @RequestParam(name = "name") String name,
                                  @RequestParam(name = "priority") Integer priority,
                                  @RequestParam(name = "testLab") String testLab,
                                  @RequestParam(name = "author") String author) {
        testDesignService.transformDesignToRun(id, name, testLab, priority, author);
    }

    @GetMapping("/title/{id}")
    public String getTitle(@PathVariable("id") String id) {
        return testDesignService.getTitle(id);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseEnvelope<String>> handelError(Exception e) {
        e.printStackTrace();
        Integer code = null;
        if (e.getClass() == MissingServletRequestParameterException.class)
            code = 400;
        else if (e.getClass() == AuthException.class)
            code = 401;
        else if (e.getClass() == StatusException.class && e.getClass() == LeafException.class)
            code = 402;
        else if (e.getClass() == LeafException.class)
            code = 403;
        else
            code = 500;
        ResponseEnvelope<String> result = new ResponseEnvelope<String>(code, e.getMessage(), null);
        return new ResponseEntity<ResponseEnvelope<String>>(result, HttpStatus.valueOf(500));
    }
}
